import { post } from './url'
import store from '@/store'
export const open = function(red_id){
   return new Promise((success) => {
       setTimeout(() => {
            post("open", {
                red_id,
                share_id: window.louie.share_id,
                area: store.state.area
            })
                .then((res) => {
                    success(res)
                })
       },500)
   })
}

export const openCheck = function(vue,red) {
    // 红包区域限制
    if(red.area_type !== '0') {
        // 用户定位的省市区是否包含在红包限制区域内
        let red_ares = red.area.split('/').pop()
        if(!store.state.area.includes(red_ares)){
            vue.$createToast({
                time: 2000,
                txt: '仅支持' + red_ares + '的用户开红包',
                type: 'error',
                mask: true
            }).show()
            return false;
        }
    }


    // 红包时间验证
    if(red.time_type === '2') {
        let now_timestamp = new Date().getTime() / 1000
        if(now_timestamp < red.time.start) {
            vue.$createToast({
                time: 2000,
                txt: '红包正在准备中',
                type: 'error',
                mask: true
            }).show()
            return false
        } else if(now_timestamp >= red.time.end){
            vue.$createToast({
                time: 2000,
                txt: '红包已过期',
                type: 'error',
                mask: true
            }).show()
            return false
        }
    }
    return true;
}
